//
//  MakeScoreView.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/12.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "MakeScoreView.h"

@interface MakeScoreView()


@end

@implementation MakeScoreView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)init{
    self = [super init];
    if (self) {
        
        _starSpace = 15;
        _starWidth = 20;
        
        [self initControl];
    }
    return self;
}

- (void)initControl{
    
    [self addSubview:self.titleLabel];
    
    for (int i=0; i<5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"foreStar"] forState:UIControlStateNormal];
        button.tag = 100+i;
        [self addSubview:button];
        [button buttonClick:^{
           
            [self handleSelect:button.tag];
        }];
    }
}

- (void)layoutSubviews{
  
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.centerY.equalTo(self.centerY);
        make.height.equalTo(@30);
        make.width.equalTo(@65);
    }];
    
    UIButton *lastBtn = nil;
    for (int i=0; i<5; i++) {
        
        UIButton *button = [self viewWithTag:100+i];
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(self.starWidth));
            make.centerY.equalTo(self.titleLabel.centerY);
            if (lastBtn==nil) {
                make.left.equalTo(self.titleLabel.right).offset(self.starSpace);
            }
            else{
                make.left.equalTo(lastBtn.right).offset(self.starSpace);
            }
        }];
        lastBtn = button;
    }
}

- (void)handleSelect:(NSInteger)index{
    
    if (_scoreLevel==index) {
        return;
    }
    _scoreLevel = index;
    
    
    for (int i=0; i<5; i++) {
        
         UIButton *button = [self viewWithTag:100+i];
        if (i<=index) {
              [button setBackgroundImage:[UIImage imageNamed:@"foreStar"] forState:UIControlStateNormal];
        }
        else{
              [button setBackgroundImage:[UIImage imageNamed:@"layerStar"] forState:UIControlStateNormal];
        }
    }
}
#pragma mark getter
- (UILabel*)titleLabel{
    if (_titleLabel==nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = kTitleColor;
        _titleLabel.adjustsFontSizeToFitWidth  = YES;
    }
    return _titleLabel;
}

@end
